package com.tom.angel.action;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.tom.angel.model.User;
import com.tom.angel.service.UserServiceI;

@SuppressWarnings("serial")
@ParentPackage("basePackage")
@Namespace("/") 
@Action(value="loginAction")
@Results( { @Result(name = "success", location = "/index.jsp"), 
@Result(name = "error", location = "/error.jsp") }) 
public class LoginAction extends ActionSupport{
	private String userName;
	private String passWord;
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	//自动注入UserService
	private UserServiceI userService;
	public UserServiceI getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	public String login() {
		HttpServletRequest request =  ServletActionContext.getRequest();
		User user =null;
		
		if(userService.getUser(userName, passWord) != null){
			user = userService.getUser(userName, passWord);
		}else{
			return ERROR;
		}
		
		request.setAttribute("user", user);
		return SUCCESS;
	}
	
	/**
	 * 测试json菜单树的方法
	 */
//	public void getTree() throws IOException{
//		List<Test> list = new ArrayList<Test>();
//		for(int i=0;i<10;i++){
//			Test test = new Test();
//			test.setId("1");
//			test.setText("aa");
//			list.add(test);
//		}
//		
//		ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(list));
//	}
	
}
